<?php
include ('config.php');
include 'checkpostandget.php';

function sel_sql($table,$sel_field,$where,$alert=0){
	global $BIAOTOU;
	$sql="select $sel_field from $BIAOTOU$table where $where";
	if($alert==1){
	    echo $sql;
	}
	$query=mysql_query($sql);
	if($query){
		while($row=mysql_fetch_array($query)){
			if(strpos($sel_field,",") or $sel_field=="*"){
			    $arr=$row;
			}
			else{
			    $arr=$row[$sel_field];
			}
		}
	}
	return $arr;	
}

function update_sql($table,$field_arr,$where,$xs,$alert=0){ //$xs 0表示更新 1表示同加  2表示同减,$alert默认为0,1表示打印sql语句,2表示先执行在打印
    global $BIAOTOU;
	$sql="";
	foreach($field_arr as $k=>$v){
		if($xs==0){
		    $sql="`$k`='$v',".$sql;
		}
		elseif($xs==1){
		    $sql="`$k`=`$k`+'$v',".$sql;
		}
		elseif($xs==2){
		    $sql="`$k`=`$k`-'$v',".$sql;
		}
	}
	$sql=substr($sql,0,strlen($sql)-1);
	$sql="update $BIAOTOU$table set $sql where $where;";
	if($alert==0){
		mysql_query($sql);
	}
	elseif($alert==1){
		echo $sql;
	}
	elseif($alert==2){
		mysql_query($sql);
	    echo $sql;
	}
}


function insert_one_sql($table,$field_arr,$alert=0){
    global $BIAOTOU;
	$field="";
	$values="";
	foreach($field_arr as $k=>$v){
		$field = "`".$k."`,".$field;
		$values = "'".$v."',".$values;
		}
	$field=substr($field,0,strlen($field)-1);
	$values=substr($values,0,strlen($values)-1);
	$sql="insert into $BIAOTOU$table($field) values ($values);";
	/*$fp = fopen('url.txt', 'a');
fwrite($fp, $sql . "\n\r");
fclose($fp);*/
	$query=mysql_query($sql);
	if($alert==0){
	    if($query) return 1;
	    else return mysql_error();
	}
	elseif($alert==1){
	    echo $sql;
	}
	elseif($alert==2){
		echo $sql;
	    if($query) return 1;
	    else return mysql_error();
	}

}

function inc_fanli($ddusername, $fxje, $tgje, $order_code,$merchant_id) {
	if(JIFENOPEN==1 && JIFENBL>0){
		$jifen=round($fxje*JIFENBL);
		if($jifen>0){
			$msg_tabao_jifen=$jifen."积分！";
		}
	}
	else{
		$jifen=0;
	}
	$field_arr=array('money'=>$fxje,'jifen'=>$jifen,'dengji'=>1);
	update_sql('user', $field_arr, "ddusername='$ddusername'", 1); //增加会员金额，积分和等级

	//用户消息
	$title = '您获得了新的商城交易返米！';
	$trade_id = $order_code;
	$msg_tabao = "您获得了新的交易返米，".$merchant_id."商城订单号" . $order_code . "返" . ($fxje*100).'米！'.$msg_tabao_jifen;
	$filed_arr = array (
		'title' => $title,
		'content' => $msg_tabao,
		'addtime' => date('Y-m-d H:i:s'
	), 'see' => 0, 'ddusername' => $ddusername, 'senduser' => '网站客服');
	insert_one_sql("msg", $filed_arr);

	//用户明细
	$shijian = "商城交易返现";
	$memo = $merchant_id."交易号$order_code";
	$filed_arr = array (
		'ddusername' => $ddusername,
		'shijian' => $shijian,
		'addtime' => date('Y-m-d H:i:s'
	), 'je' => $fxje,'jifen'=>$jifen, 'memo' => $memo);
	insert_one_sql("mingxi", $filed_arr);

	//求推荐人
	$tjrid = sel_sql("user", "tjr", "ddusername='$ddusername'");
	if ($tjrid > 0) {
		$tjrname = sel_sql("user", "ddusername", "Id='$tjrid'");
		//增加推荐人佣金
		$field_arr = array (
			'money' => $tgje
		);
		update_sql("user", $field_arr, "Id='$tjrid'", 1);

		//用户消息
		$title = '您获得了新的推广佣金！';
		$msg_taobaotuiguang = "您获得了新的推广佣金" . ($tgje*100)."米";
		$filed_arr = array (
			'title' => $title,
			'content' => $msg_taobaotuiguang,
			'addtime' => date('Y-m-d H:i:s'
		), 'see' => 0, 'ddusername' => $tjrname, 'senduser' => '网站客服');
		insert_one_sql("msg", $filed_arr);

		//用户明细
		$shijian = "推广佣金";
		$memo = "交易人$ddusername";
		$filed_arr = array (
			'ddusername' => $tjrname,
			'shijian' => $shijian,
			'addtime' => date('Y-m-d H:i:s'
		), 'je' => $tgje, 'memo' => $memo);
		insert_one_sql("mingxi", $filed_arr);
	}
}

function create($dir) {
	if (!is_dir($dir)) {
		$temp = explode('/', $dir);
		$cur_dir = '';
		for ($i = 0; $i < count($temp); $i++) {
			$cur_dir .= $temp[$i] . '/';
			if (!is_dir($cur_dir)) {
				@ mkdir($cur_dir);
			}
		}
	}
}

function rep($str){
    $re="/[^\d]/";
    return preg_replace($re,"",$str);
}

$unique_id=$_GET['unique_id']; //数据唯一编号
$merchant_id=$_GET['action_name']; //活动名
$merchant_id=iconv('gbk','utf-8',$merchant_id);
$merchant_id=trim(preg_replace('/cps/i','',$merchant_id));
$order_code=$_GET['order_no']; //订单编号
$order_time=$_GET['order_time']?date("Ymd His",strtotime($_GET['order_time'])):date('Ymd His'); //下单时间
$product_code=trim(iconv('gbk', 'utf-8', $_GET['prod_id'])); //商品编号
$prod_name=$_GET['prod_name']; //商品名称
if($product_code=='汇总'){ //商品数量
	$item_count=1;
}
else{
	$item_count=$_GET['prod_count'];
}
$item_price=$_GET['prod_money']; //商品单价
$sales = $item_price * $item_count; //总额
$comm_type=$_GET['comm_type']; //佣金类型
$commission=$_GET['commision']; //网站主佣金
$u_id=$_GET['feed_back']; //反馈标签

if(strstr($u_id,'_')){
    $abc=explode('_',$u_id);
	$u_id=$abc[0];
}
else{
	$u_id=rep($u_id);
}
if($u_id=='null'){
	$u_id=0;
}

$u_id=(int)$u_id;

$dduser = sel_sql("user", "ddusername,dengji", "Id='$u_id'");
$ddusername = $dduser['ddusername'];
$dengji = $dduser['dengji'];
$fxje = mall_ck_fxje($commission, $dengji); //会员返利金额
$tgje = round($commission * TGBL, 2); //给会员的推广佣金

$status=$_GET['status']; //订单状态
switch($status){
    case 'R': $stat_desc='未核对';
	break;
	case 'A': $stat_desc='核对有效';
	break;
	case 'F': $stat_desc='无效订单';
	break;
}

$create_date=$_GET['create_date']; //订单数据返回到亿起发的时间
$action_id=$_GET['action_id']; //活动编号
$sid=$_GET['sid']; //网站主编号
$wid=$_GET['wid']; //站点编号
$prod_type=$_GET['prod_type']; //商品类型
$chkcode=$_GET['chkcode']; //验证密钥   action_id+order_no+prod_money+order_time+ 站点push数据key值

$code=md5($_GET['action_id'].$_GET['order_no'].$_GET['prod_money'].$_GET['order_time'].MIYUE);

$get=var_export($_GET, true);
$dirname ='../data/yiqifa/'. date("Y").'/'.date('md');
create($dirname);
$fp = fopen($dirname.'/yiqifa.txt', 'a');
fwrite($fp, $get . "\n\r");
fclose($fp);

if($code!=$chkcode){
    exit('err code');
}

//添加交易记录
$mall_order = sel_sql("mall_order", "id,stat_desc,fxje,commission", "unique_id='$unique_id'"); //用数据唯一编号验证该交易是否存在于数据库
if ($mall_order['id'] == '') { //交易不存在
	$field_arr = array (
		'unique_id' => $unique_id,
		'lm' => 'yiqifa',
		'order_time' => $order_time,
		'product_code' => $product_code,
		'merchant_id' => $merchant_id,
		'u_id' => $u_id,
		'order_code' => $order_code,
		'item_count' => $item_count,
		'item_price' => $item_price,
		'sales' => $sales,
		'commission' => $commission,
		'stat_desc' => $stat_desc,
		'bill_yyyymmdd' => $bill_yyyymmdd,
		'fxje' => $fxje,
		'ddusername' => $ddusername,
		'bill_yyyymmdd'=>date('Y-m-d H:i:s')
	);
	if($stat_desc=='核对有效'){
	    $field_arr['qrsj']=date('Y-m-d H:i:s');
	}
	insert_one_sql("mall_order", $field_arr,0);
	
	if($stat_desc=='核对有效'){
		if($ddusername!='' and $fxje>0){
		    inc_fanli($ddusername, $fxje, $tgje, $order_code, $merchant_id);
		}
	}
    echo 1;
}
elseif($mall_order['id']>0 and $mall_order['stat_desc']=='未核对' and $stat_desc=='核对有效'){
	$field_arr_order = array (
		'stat_desc' => '核对有效',
		'qrsj' => date('Y-m-d H:i:s'), 
		'fxje' => $fxje,
		'commission' => $commission,
		'bill_yyyymmdd' => $bill_yyyymmdd
	);
	update_sql('mall_order', $field_arr_order, "id='".$mall_order['id']."'",0);
	$tgje = round($commission * TGBL, 2);
	if($fxje>0){
	    inc_fanli($ddusername, $fxje, $tgje, $order_code, $merchant_id);
	}
	echo 0;
}
elseif($mall_order['id']>0 and $mall_order['stat_desc']=='未核对' and $stat_desc=='无效订单'){
	$field_arr_order=array('stat_desc'=>'无效订单','qrsj'=>date('Y-m-d H:i:s'));
	update_sql('mall_order', $field_arr_order, "id='".$mall_order['id']."'",0);
    echo 0;
}
elseif($mall_order['id']>0 and $mall_order['stat_desc']<>'未核对'){
    echo 0;
}
else{
    echo -1;
}

mysql_close();
?>